package pub.tbc.atps.util.okhttp.builder;

import pub.tbc.toolkit.Loops;
import pub.tbc.toolkit.function.BiConsumer;

import java.util.Map;

/**
 * @author tbc on 2016/12/2 13:19:50.
 */
public class HttpUrlBuilder {
    String url = "";

    private HttpUrlBuilder() {
    }

    public static HttpUrlBuilder builder() {
        return new HttpUrlBuilder();
    }

    public HttpUrlBuilder url(String url) {
        this.url = url + this.url;
        return this;
    }

    public HttpUrlBuilder pathParam(Map<String, Object> param) {
        StringBuilder sb = new StringBuilder();
        Loops.forEach(param, new BiConsumer<String, Object>() {
            @Override
            public void accept(String k, Object v) {
                sb.append("/").append(v);
            }
        });
        this.url = this.url + sb.toString();
        return this;
    }

    public HttpUrlBuilder httpParam(Map<String, Object> param) {
        StringBuilder sb = new StringBuilder("?");
        Loops.forEach(param, new BiConsumer<String, Object>() {
            @Override
            public void accept(String k, Object v) {
                sb.append(k).append("=").append(v);
            }
        });
        this.url = this.url + sb.toString();
        return this;
    }

    public String build() {
        return this.url;
    }
}
